package com.jdd.enterprise.domain.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.jdd.enterprise.api.dto.PageRequest;
import com.jdd.enterprise.api.vo.*;
import com.jdd.enterprise.domain.entity.*;
import com.jdd.enterprise.domain.mapper.*;
import com.jdd.enterprise.util.PageUtil;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DishSpuService extends  ServiceImpl<DishSpuMapper, DishSpu> {
    @Autowired
    private DishSpuCookingMethodRelationMapper dishSpuCookingMethodRelationMapper;


    @Autowired
    private CookingMethodMapper cookingMethodMapper;
    @Autowired
    private StallMapper stallMapper;
    @Autowired
    private DishSpuStallRelationMapper dishSpuStallRelationMapper;
    @Autowired
    private DishExtraToppingsRelationMapper dishExtraToppingsRelationMapper;

    @Autowired
    private DishExtraToppingsRelationService dishExtraToppingsRelationService;


    @Autowired
    private DishSkuMapper dishSkuMapper;
    @Autowired
    private DishSpuMapper dishSpuMapper;

    public PageResponse<DishSpuVO> spuList(PageRequest<DishSpu> request) {
        DishSpu dishSpu = request.getParam();
        PageResponse<DishSpuVO> page = PageUtil.createPage(request.getCurrentPage(), request.getPageSize());
        Page<DishSpu> dishSpuPage = new Page<>(request.getCurrentPage(), request.getPageSize());
        LambdaQueryWrapper<DishSpu> wrapper = Wrappers.lambdaQuery(DishSpu.class);
        if (StringUtils.isNotBlank(dishSpu.getDishSpuName())) {
            wrapper.like(DishSpu::getDishSpuName, dishSpu.getDishSpuName());
//            wrapper.like(DishSpu::getMnemonicCode, dishSpu.getMnemonicCode());
        }
        Page<DishSpu> page1 = this.page(dishSpuPage, wrapper);
//        List<DishSpu> dishSpus = dishSpuMapper.selectList(wrapper);

        List<DishSpu> dishSpus = page1.getRecords();


        List<DishSpuVO> dishSpuVOS = new ArrayList<>();

        //要查sku!!,是根据sku的dish_spu_code去查询的
        for (DishSpu spu : dishSpus) {

            LambdaQueryWrapper<DishSku> wrapper2 = Wrappers.lambdaQuery(DishSku.class);
            wrapper2.like(DishSku::getDishSpuCode,spu.getDishSpuCode());

            List<DishSku> dishSkus = dishSkuMapper.selectList(wrapper2);

            DishSpuVO dishSpuVO = new DishSpuVO();
            BeanUtils.copyProperties(spu,dishSpuVO);
            dishSpuVO.setSkuList(dishSkus);

            dishSpuVOS.add(dishSpuVO);



        }


//        PageUtil.refreshData(page, page1.getTotal(), dishSpus);
        PageUtil.refreshData(page,page1.getTotal(),dishSpuVOS);

        return page;
    }
/*
        根据菜品分类去查询菜品(s)
 */
    public List<DishSpu>   selectDishSpuByCategory(String dishCategory){
        LambdaQueryWrapper<DishSpu> wrapper = Wrappers.lambdaQuery(DishSpu.class);
        wrapper.eq(DishSpu::getDishCategory,dishCategory);
        List<DishSpu> dishSpus = dishSpuMapper.selectList(wrapper);
        return  dishSpus;
    }


/*
菜品排序
 */
    public void sorting(Map<Integer, Integer> map) {
        ArrayList<DishSpu> dishSpus = new ArrayList<>();
        for (Integer i : map.keySet()) {
            DishSpu dishSpu = new DishSpu();
            dishSpu.setId(i);
            dishSpu.setDishSort(map.get(i));
            dishSpus.add(dishSpu);
        }
        updateBatchById(dishSpus);

    }

    public void createDishSpu(DishSpuDTO param) {




        //first  add spu
        DishSpu dishSpu = new DishSpu();
        BeanUtils.copyProperties(param,dishSpu);
        save(dishSpu);

        //second  add sku
        List<DishSku> dishSkuList = param.getDishSkuList();
        dishSkuMapper.insertBatch(dishSkuList);

    //three  add  dish_extra_relations

        List<DishSpu> extraToppings = param.getExtraToppings();
        dishExtraToppingsRelationMapper.insertBatch(extraToppings);


    }

    public List<SpuAndSkuVO> spuAndSku(PageRequest<DishSpu> request) {
        LambdaQueryWrapper<DishSku> wrapper = Wrappers.lambdaQuery(DishSku.class);
        List<DishSku> dishSkus = dishSkuMapper.selectList(wrapper);
        List<SpuAndSkuVO> spuAndSkuVOS = new ArrayList<>();
        for (DishSku sku : dishSkus) {
            DishSpu hisSpu = getHisSpu(sku.getDishSpuCode());
            SpuAndSkuVO spuAndSkuVO = new SpuAndSkuVO();
        spuAndSkuVO.setDishCategory(hisSpu.getDishCategory());
        spuAndSkuVO.setPrice(sku.getPrice());
        spuAndSkuVO.setBelongTo(hisSpu.getBelongTo());
        spuAndSkuVO.setDishSpecificationName(sku.getDishSpecificationName());
        spuAndSkuVO.setDishSpuCode(sku.getDishSpuCode());
        spuAndSkuVO.setVipPrice(sku.getVipPrice());
        spuAndSkuVO.setEstimatedCost(sku.getEstimatedCost());
        spuAndSkuVO.setDishUnit(hisSpu.getDishUnit());
        spuAndSkuVO.setSaleFlag(hisSpu.getSaleFlag());
        spuAndSkuVO.setMnemonicCode(hisSpu.getMnemonicCode());
        spuAndSkuVO.setDishSpuName(hisSpu.getDishSpuName());
        spuAndSkuVO.setDishType(hisSpu.getDishType());
        spuAndSkuVO.setPinyinCode(hisSpu.getPinyinCode());
        spuAndSkuVO.setPrintedStall(getPrintedStall(sku.getDishSpuCode()));
        spuAndSkuVO.setCookingMethods(getCookingMethods(sku.getDishSpuCode()));
        spuAndSkuVO.setDishCornerMark(hisSpu.getDishCornerMark());
        spuAndSkuVO.setDishPictureUrl(hisSpu.getDishPictureUrl());
        spuAndSkuVO.setDishDescriptionShort(hisSpu.getDishDescriptionShort());
        spuAndSkuVO.setDishDescriptionLong(hisSpu.getDishDescriptionLong());
        spuAndSkuVOS.add(spuAndSkuVO);
        }
        return  spuAndSkuVOS;
    }

    private String getCookingMethods(String dishSpuCode) {
        LambdaQueryWrapper<DishSpuCookingMethodRelation> wrapper1 = Wrappers.lambdaQuery(DishSpuCookingMethodRelation.class);
        wrapper1.eq(DishSpuCookingMethodRelation::getDishSpuCode,dishSpuCode);
        wrapper1.select(DishSpuCookingMethodRelation::getMethodId);
        List<Object> objects1 = dishSpuCookingMethodRelationMapper.selectObjs(wrapper1);
        List<Integer> integers = objects1.stream().map(object -> (Integer) object).collect(Collectors.toList());
        LambdaQueryWrapper<CookingMethod> wrapper = Wrappers.lambdaQuery(CookingMethod.class);
        wrapper.in(CookingMethod::getId,integers);
        wrapper.select(CookingMethod::getMethodName);
        List<Object> objects = cookingMethodMapper.selectObjs(wrapper);
        List<String> collect = objects.stream().map(object -> (String) object).collect(Collectors.toList());
        String join = String.join("、", collect);
        return join;
    }

    private DishSpu getHisSpu(String dishSpuCode) {
        LambdaQueryWrapper<DishSpu> wrapper= Wrappers.lambdaQuery(DishSpu.class);
        wrapper.eq(DishSpu::getDishSpuCode,dishSpuCode);

        DishSpu one = getOne(wrapper);

        return one;
    }


    private String  getPrintedStall(String  dishSpuCode){
        LambdaQueryWrapper<Stall> wrapper1 = Wrappers.lambdaQuery(Stall.class);
        LambdaQueryWrapper<DishSpuStallRelation> wrapper2 = Wrappers.lambdaQuery(DishSpuStallRelation.class);
        wrapper2.select(DishSpuStallRelation::getStallId);
        wrapper2.eq(DishSpuStallRelation::getDishSpuCode,dishSpuCode);
        List<Object> objects1 = dishSpuStallRelationMapper.selectObjs(wrapper2);
        List<Integer> integers = objects1.stream().map(object -> (Integer) object).collect(Collectors.toList());
        wrapper1.in(Stall::getId,integers);
        wrapper1.select(Stall::getStallName);
        List<Object> objects = stallMapper.selectObjs(wrapper1);
        List<String> collect = objects.stream().map(object -> (String) object).collect(Collectors.toList());
        String join = String.join("、", collect);
        return join;
    }


    public List<TakeoutBoxVO> showTakeoutBoxes() {

        LambdaQueryWrapper<DishSpu> wrapper = Wrappers.lambdaQuery(DishSpu.class);


        wrapper.eq(DishSpu::getDishType,"餐盒");
        List<DishSpu> dishSpus = dishSpuMapper.selectList(wrapper);


        for (DishSpu spu : dishSpus) {

            TakeoutBoxVO takeoutBoxVO = new TakeoutBoxVO();


            takeoutBoxVO.setTakeoutBoxName(spu.getDishSpuName());
            takeoutBoxVO.setBelongTo(spu.getBelongTo());
//            takeoutBoxVO.setPrice();






        }


        return null;
    }
}











